package com.vimi8.ebb.api.datareport;

import com.vimi8.ebb.configuration.HttpResponseEntity;
import com.vimi8.ebb.dto.EbbDataReportCollectivityDto;
import com.vimi8.ebb.dto.TradeSituationDto;
import com.vimi8.ebb.model.Error;
import com.vimi8.ebb.service.DataReportCollectivityService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by think on 2017/4/10.
 */
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.SpringCodegen", date = "2017-04-10T16:53:54.080Z")
@Controller
public class DataReportCollectivityController implements DataReportCollectivityApi {
    @Autowired
    private DataReportCollectivityService dataReportCollectivityService;
    @Override
    public HttpResponseEntity<EbbDataReportCollectivityDto> getDataReports(
            @ApiParam(value = "year（年—参数为纯数字，如：2017）",required = true) @RequestParam(value="year",required = true)int year,
            @ApiParam(value = "quarter（季度—参数为数字，如：3）",required = true) @RequestParam(value="quarter",required = true)int quarter
    ) {
        EbbDataReportCollectivityDto collectivityDto=new EbbDataReportCollectivityDto();
        try{
            String timeStart="";
            String timeEnd="";
            if(quarter==1){
                timeStart = String.valueOf(year)+"-01-01";
                timeEnd = String.valueOf(year)+"-04-01";
            }else if(quarter==2){
                timeStart = String.valueOf(year)+"-04-01";
                timeEnd = String.valueOf(year)+"-07-01";
            }else if(quarter==3){
                timeStart = String.valueOf(year)+"-07-01";
                timeEnd = String.valueOf(year)+"-10-01";
            }else{
                timeStart = String.valueOf(year)+"-10-01";
                timeEnd = String.valueOf(year+1)+"-01-01";
            }
            collectivityDto=dataReportCollectivityService.getCollectivityTotal(timeStart,timeEnd);
        }catch (Exception e){
            return new HttpResponseEntity(new Error().error("sql error").errorDescription(e.getMessage()),HttpStatus.SERVICE_UNAVAILABLE);
        }
        return new HttpResponseEntity<EbbDataReportCollectivityDto>(collectivityDto, HttpStatus.OK);
    }

    @Override
    public HttpResponseEntity<List<TradeSituationDto>> getAnalysisDataReportsByType(@ApiParam(value = "year（年—参数为纯数字，如：2017）", required = true) @RequestParam(value = "year", required = true) int year, @ApiParam(value = "quarter（季度—参数为数字，如：3）", required = true) @RequestParam(value = "quarter", required = true) int quarter) {
        List<TradeSituationDto> tradeSituationDtoList=new ArrayList<TradeSituationDto>();
        try {
            String timeStart="";
            String timeEnd="";
            if(quarter==1){
                timeStart = String.valueOf(year)+"-01-01";
                timeEnd = String.valueOf(year)+"-04-01";
            }else if(quarter==2){
                timeStart = String.valueOf(year)+"-04-01";
                timeEnd = String.valueOf(year)+"-07-01";
            }else if(quarter==3){
                timeStart = String.valueOf(year)+"-07-01";
                timeEnd = String.valueOf(year)+"-10-01";
            }else{
                timeStart = String.valueOf(year)+"-10-01";
                timeEnd = String.valueOf(year+1)+"-01-01";
            }
            tradeSituationDtoList=dataReportCollectivityService.getAnalysisDataReportsByType(timeStart,timeEnd);
        }catch (Exception e){
            return new HttpResponseEntity(new Error().error("sql error").errorDescription(e.getMessage()),HttpStatus.SERVICE_UNAVAILABLE);
        }
        return new HttpResponseEntity<List<TradeSituationDto>>(tradeSituationDtoList, HttpStatus.OK);
    }

    @Override
    public HttpResponseEntity<List<TradeSituationDto>> getMonthAndQuarterDataAnalysisReports(@ApiParam(value = "year（年—参数为纯数字，如：2017）", required = true) @RequestParam(value = "year", required = true) int year, @ApiParam(value = "quarter（季度—参数为数字，如：3）", required = true) @RequestParam(value = "quarter", required = true) int quarter) {
        List<TradeSituationDto> tradeSituationDtoList=new ArrayList<TradeSituationDto>();
        List<HashMap<String,String>> mapList=new ArrayList<>();
        try {
            String timeStart1="", timeEnd1="",timeStart2="", timeEnd2="",timeStart3="", timeEnd3="",timeStart="", timeEnd="";
            if(quarter==1){
                HashMap<String , String> map1 = new HashMap<String , String>();
                timeStart1 = String.valueOf(year)+"-01-01";
                timeEnd1 = String.valueOf(year)+"-02-01";
                map1.put("timeStart",timeStart1);
                map1.put("timeEnd",timeEnd1);
                map1.put("type","1月");
                mapList.add(map1);
                HashMap<String , String> map2 = new HashMap<String , String>();
                timeStart2 = String.valueOf(year)+"-02-01";
                timeEnd2 = String.valueOf(year)+"-03-01";
                map2.put("timeStart",timeStart2);
                map2.put("timeEnd",timeEnd2);
                map2.put("type","2月");
                mapList.add(map2);
                HashMap<String , String> map3 = new HashMap<String , String>();
                timeStart3 = String.valueOf(year)+"-03-01";
                timeEnd3 = String.valueOf(year)+"-04-01";
                map3.put("timeStart",timeStart3);
                map3.put("timeEnd",timeEnd3);
                map3.put("type","3月");
                mapList.add(map3);
                HashMap<String , String> map = new HashMap<String , String>();
                timeStart = String.valueOf(year)+"-01-01";
                timeEnd = String.valueOf(year)+"-04-01";
                map.put("timeStart",timeStart);
                map.put("timeEnd",timeEnd);
                map.put("type","第一季度");
                mapList.add(map);

            }else if(quarter==2){
                HashMap<String , String> map1 = new HashMap<String , String>();
                timeStart1 = String.valueOf(year)+"-04-01";
                timeEnd1 = String.valueOf(year)+"-05-01";
                map1.put("timeStart",timeStart1);
                map1.put("timeEnd",timeEnd1);
                map1.put("type","4月");
                mapList.add(map1);
                HashMap<String , String> map2 = new HashMap<String , String>();
                timeStart2 = String.valueOf(year)+"-05-01";
                timeEnd2 = String.valueOf(year)+"-06-01";
                map2.put("timeStart",timeStart2);
                map2.put("timeEnd",timeEnd2);
                map2.put("type","5月");
                mapList.add(map2);
                HashMap<String , String> map3 = new HashMap<String , String>();
                timeStart3 = String.valueOf(year)+"-06-01";
                timeEnd3 = String.valueOf(year)+"-07-01";
                map3.put("timeStart",timeStart3);
                map3.put("timeEnd",timeEnd3);
                map3.put("type","6月");
                mapList.add(map3);
                HashMap<String , String> map = new HashMap<String , String>();
                timeStart = String.valueOf(year)+"-04-01";
                timeEnd = String.valueOf(year)+"-07-01";
                map.put("timeStart",timeStart);
                map.put("timeEnd",timeEnd);
                map.put("type","第二季度");
                mapList.add(map);
            }else if(quarter==3){
                HashMap<String , String> map1 = new HashMap<String , String>();
                timeStart1 = String.valueOf(year)+"-07-01";
                timeEnd1 = String.valueOf(year)+"-08-01";
                map1.put("timeStart",timeStart1);
                map1.put("timeEnd",timeEnd1);
                map1.put("type","7月");
                mapList.add(map1);
                HashMap<String , String> map2 = new HashMap<String , String>();
                timeStart2 = String.valueOf(year)+"-08-01";
                timeEnd2 = String.valueOf(year)+"-09-01";
                map2.put("timeStart",timeStart2);
                map2.put("timeEnd",timeEnd2);
                map2.put("type","8月");
                mapList.add(map2);
                HashMap<String , String> map3 = new HashMap<String , String>();
                timeStart3 = String.valueOf(year)+"-09-01";
                timeEnd3 = String.valueOf(year)+"-10-01";
                map3.put("timeStart",timeStart3);
                map3.put("timeEnd",timeEnd3);
                map3.put("type","9月");
                mapList.add(map3);
                HashMap<String , String> map = new HashMap<String , String>();
                timeStart = String.valueOf(year)+"-07-01";
                timeEnd = String.valueOf(year)+"-10-01";
                map.put("timeStart",timeStart);
                map.put("timeEnd",timeEnd);
                map.put("type","第三季度");
                mapList.add(map);

            }else{
                HashMap<String , String> map1 = new HashMap<String , String>();
                timeStart1 = String.valueOf(year)+"-10-01";
                timeEnd1 = String.valueOf(year)+"-11-01";
                map1.put("timeStart",timeStart1);
                map1.put("timeEnd",timeEnd1);
                map1.put("type","10月");
                mapList.add(map1);
                HashMap<String , String> map2 = new HashMap<String , String>();
                timeStart2 = String.valueOf(year)+"-11-01";
                timeEnd2 = String.valueOf(year)+"-12-01";
                map2.put("timeStart",timeStart2);
                map2.put("timeEnd",timeEnd2);
                map2.put("type","11月");
                mapList.add(map2);
                HashMap<String , String> map3 = new HashMap<String , String>();
                timeStart3 = String.valueOf(year)+"-12-01";
                timeEnd3 = String.valueOf(year+1)+"-01-01";
                map3.put("timeStart",timeStart3);
                map3.put("timeEnd",timeEnd3);
                map3.put("type","12月");
                mapList.add(map3);
                HashMap<String , String> map = new HashMap<String , String>();
                timeStart = String.valueOf(year)+"-10-01";
                timeEnd = String.valueOf(year+1)+"-01-01";
                map.put("timeStart",timeStart);
                map.put("timeEnd",timeEnd);
                map.put("type","第四季度");
                mapList.add(map);
            }
            for(int i=0;i<mapList.size();i++){
                TradeSituationDto tradeSituationDtos=new TradeSituationDto();
                tradeSituationDtos = dataReportCollectivityService.getMonthAndQuarterDataAnalysisReports(mapList.get(i).get("timeStart"),mapList.get(i).get("timeEnd"),mapList.get(i).get("type"));
                tradeSituationDtoList.add(tradeSituationDtos);
            }

        }catch (Exception e){
            return new HttpResponseEntity(new Error().error("sql error").errorDescription(e.getMessage()),HttpStatus.SERVICE_UNAVAILABLE);
        }
        return new HttpResponseEntity<List<TradeSituationDto>>(tradeSituationDtoList, HttpStatus.OK);
    }
}
